var MainPage_Warn = window.NameSpace || {};

MainPage_Warn.createWarnmon = function(divId) {
	var dom = document.getElementById(divId);
	MP_GL_WARN = echarts.init(dom);
	
	
	MainPage_Warn.loadWarn();

	return MP_GL_WARN;

}

MainPage_Warn.loadWarn = function(){
	var option = {
			color : [ '#00a1ff', '#ffae00', '#4ed852'],
			legend : {
				x : 'right',
				y : 'top',
				data : [ '机载', '地面', '网络' ],
				textStyle : {
					color : '#666',
					fontSize : 9
				}

			},
			calculable : true,
			grid : {
				y : 25,
				y2 : 30,
				x : 50,
				x2 : 25
			},
			xAxis : [ {
				type : 'category',
				data : [],
				axisLabel : {
					textStyle : {
						color : '#666',
						fontSize : 9
					}
				}
			} ],
			yAxis : [ {
				type : 'value',
				axisLabel : {
					formatter : '{value}次',
					textStyle : {
						color : '#666',
						fontSize : 9
					}
				},

			} ],
			series : [ {
				name : '机载',
				type : 'line',
				data : [ ],
				markPoint : {
					data : [ {
						type : 'max',
						name : '最大值'
					}, {
						type : 'min',
						name : '最小值'
					} ]
				}
			}, {
				name : '地面',
				type : 'line',
				data : [],
				markPoint : {
					data : [ {
						type : 'max',
						name : '最大值'
					}, {
						type : 'min',
						name : '最小值'
					} ]
				}
			}, {
				name : '网络',
				type : 'line',
				data : [],
				markPoint : {
					data : [ {
						type : 'max',
						name : '最大值'
					}, {
						type : 'min',
						name : '最小值'
					} ]
				}
			} ]
		};
	
	$.ajax({
		url : "/mainpage/loadWarn",
		async : true,
		success : function(data) {
			if (data != null) {
				MP_GL_WARN.showLoading();
				var tmp = data.LOCAL!=undefined?data.LOCAL:[];
				// MP_LASTUPDATE_TIME = tmp[0].create_time;

				var timeData = [];
				for (var i = tmp.length-1; i>=0  ; i--) {
					var xTime = tmp[i].create_time;
					xTime = xTime.toDate().format('hh tt');
					timeData.push(xTime);
				}
				// console.log(timeData.length)
				if (timeData.length > 0) {
					option.xAxis[0].data = timeData;
				}

				// 机载
				var plane = data.PLANE;
				// console.log(JSON.stringify(int));
				var planeData = [];
				for (var i = plane.length-1; i >=0; i--) {
					var value = plane[i].value;
					planeData.push(value);
				}
				if (planeData.length > 0) {
					option.series[0].data = planeData;
				}

				// 地面
				var local = data.LOCAL!=undefined?data.LOCAL:[];
				var localData = [];
				for (var i = local.length-1; i >=0; i--) {
					var value = local[i].value;
					localData.push(value);
				}
				if (localData.length > 0) {
					option.series[1].data = localData;
				}
				
				// 网络
				var net = data.NET!=undefined?data.NET:[];
				var netData = [];
				for (var i =  net.length-1; i >=0; i--) {
					var value = net[i].value;
					netData.push(value);
					// console.log(atg[i].create_time+' & '+value);
				}
				if (netData.length > 0) {
					option.series[2].data = netData;
				}	
			}

			MP_GL_WARN.setOption(option, true);
			MP_GL_WARN.hideLoading();
			
			setTimeout(MainPage_Warn.loadWarn , mp_refresh_time);
		
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			console.log('exception>>'+textStatus);
			setTimeout(MainPage_Warn.loadWarn  , mp_refresh_time);
		}
	});
	
	
}
